package com.love.marriage.userMembershipInfo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.love.marriage.common.interfaces.avoid.AvoidRepeatableCommit;
import com.love.marriage.common.interfaces.avoid.LogRestRequest;
import com.love.marriage.common.validator.Insert;
import com.love.marriage.common.validator.Update;
import com.love.marriage.common.vo.Result;
import com.love.marriage.common.vo.ResultBoolean;
import com.love.marriage.orderInfo.service.PayInfoService;
import com.love.marriage.userMembershipInfo.service.UserMemberPrivilegeService;
import com.love.marriage.userMembershipInfo.service.UserMembershipInfoService;
import com.love.marriage.userMembershipInfo.vo.ResultUserMembershipInfoVo;
import com.love.marriage.userMembershipInfo.vo.UserMembershipInfoPageVo;
import com.love.marriage.userMembershipInfo.vo.UserMembershipInfoQueryVo;
import com.love.marriage.userMembershipInfo.vo.UserMembershipInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * description:  用户会员表restApi
 * author:       mybatisPlus
 * createDate:   2024-10-17 07:29:59
 * version:      1.0.0
 */
@ApiSort(105)
@Tag(name = "用户会员表相关接口", description = "用户会员表相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/user-membership-info")
public class UserMembershipInfoController {

    private final UserMembershipInfoService userMembershipInfoService;

    private final PayInfoService payInfoService;

    private final UserMemberPrivilegeService userMemberPrivilegeService;

    @ApiOperationSupport(order = 10, author = "mybatisPlus")
    @LogRestRequest(apiName = "获取用户会员表分页")
    @Operation(summary = "获取用户会员表分页", description = "获取用户会员表分页")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = UserMembershipInfoPageVo.class
                    )))
    @PostMapping(value = "/page")
    @Parameters({
            @Parameter(name = "pageNum", description = "页码", required = false),
            @Parameter(name = "pageSize", description = "每页大小", required = false)
    })
    public Result<Page<UserMembershipInfoPageVo>> getPage(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                                          @RequestParam(value = "pageSize", required = false) Long pageSize,
                                                          @RequestBody(required = false) UserMembershipInfoVo userMembershipInfoVo) {
        return Result.success(userMembershipInfoService.getPage(pageNum, pageSize, userMembershipInfoVo));
    }

    @ApiOperationSupport(order = 20, author = "mybatisPlus")
    @LogRestRequest(apiName = "获取用户会员表详情")
    @Operation(summary = "获取用户会员表详情", description = "获取用户会员表详情")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserMembershipInfoVo.class
                    )))
    @Parameters({
            @Parameter(name = "id", description = "id", required = true, schema = @Schema(implementation = Long.class, type = "Long", example = "1"))
    })
    @GetMapping
    public Result<UserMembershipInfoVo> query(@RequestParam(value = "id") Long id) {
        return Result.success(userMembershipInfoService.queryUserMembershipInfo(id));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 25, author = "mybatisPlus")
    @LogRestRequest(apiName = "办理会员")
    @Operation(summary = "办理会员", description = "办理会员",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserMembershipInfoVo.class))
            ))
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = Result.class)))
    @PostMapping(value = "/membership")
    @Tag(name = "会员", description = "会员信息")
    public Result<WxPayUnifiedOrderV3Result.JsapiResult> membership(@RequestBody UserMembershipInfoVo userMembershipInfoVo) throws WxPayException {
        return Result.success(payInfoService.membership(userMembershipInfoVo));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 30, author = "mybatisPlus")
    @LogRestRequest(apiName = "新增用户会员表")
    @Operation(summary = "新增用户会员表", description = "新增用户会员表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserMembershipInfoVo.class))
            ))
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserMembershipInfoVo.class)))
    @PostMapping
    public Result<UserMembershipInfoVo> add(@Validated({Insert.class}) @RequestBody UserMembershipInfoVo userMembershipInfoVo) {
        return Result.success(userMembershipInfoService.addUserMembershipInfoAdmin(userMembershipInfoVo));
    }

    @ApiOperationSupport(order = 40, author = "mybatisPlus")
    @LogRestRequest(apiName = "修改用户会员表")
    @Operation(summary = "修改用户会员表", description = "修改用户会员表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserMembershipInfoVo.class))
            ))
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserMembershipInfoVo.class)))
    @PutMapping
    public Result<UserMembershipInfoVo> update(@Validated({Update.class}) @RequestBody UserMembershipInfoVo userMembershipInfoVo) {
        return Result.success(userMembershipInfoService.updateUserMembershipInfo(userMembershipInfoVo));
    }

    @ApiOperationSupport(order = 50, author = "mybatisPlus")
    @LogRestRequest(apiName = "刪除用户会员表")
    @Operation(summary = "刪除用户会员表", description = "刪除用户会员表")
    @Parameters({
            @Parameter(name = "ids", description = "ids,用，分割", required = true, schema = @Schema(implementation = String.class, type = "String", example = "1,2"))
    })
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultBoolean.class)))
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam("ids") String ids) {
        return Result.success(userMembershipInfoService.deleteUserMembershipInfo(ids));
    }

    @ApiOperationSupport(order = 60, author = "alex")
    @LogRestRequest(apiName = "添加会员权限")
    @Operation(summary = "添加会员权限", description = "添加会员权限")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultBoolean.class)))
    @Parameters({
            @Parameter(name = "userId", description = "用户id", required = false, schema = @Schema(implementation = Long.class, type = "Long", example = "1"))
    })
    @GetMapping(value = "addMemberPrivilege")
    @Tag(name = "会员", description = "会员信息")
    public Result<Boolean> addMemberPrivilege(@RequestParam(value = "userId", required = false) Long userId) {
        return Result.success(userMemberPrivilegeService.addMemberPrivilege(userId));
    }

    @ApiOperationSupport(order = 70, author = "system")
    @LogRestRequest(apiName = "导出会员信息")
    @Operation(summary = "导出会员信息", description = "按时间、会员类型、年龄段、性别、地域、身高、收入、会员号等导出会员信息",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "导出查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserMembershipInfoQueryVo.class))
            ))
    @ApiResponse(responseCode = "200", description = "Success")
    @PostMapping(value = "/export")
    public void exportUserMembershipInfo(@RequestBody(required = false) UserMembershipInfoQueryVo queryVo,
                                       HttpServletResponse response) throws Exception {
        // 将查询条件转换为UserMembershipInfoVo
        UserMembershipInfoVo userMembershipInfoVo = new UserMembershipInfoVo();
        if (queryVo != null) {
            userMembershipInfoVo.setStartTimeBegin(queryVo.getStartTimeBegin())
                    .setStartTimeEnd(queryVo.getStartTimeEnd())
                    .setEndTimeBegin(queryVo.getEndTimeBegin())
                    .setEndTimeEnd(queryVo.getEndTimeEnd())
                    .setMembershipType(queryVo.getMembershipType())
                    .setGender(queryVo.getGender())
                    .setAgeMin(queryVo.getAgeMin())
                    .setAgeMax(queryVo.getAgeMax())
                    .setProvinceId(queryVo.getProvinceId())
                    .setCityId(queryVo.getCityId())
                    .setDistrictId(queryVo.getDistrictId())
                    .setHeightMin(queryVo.getHeightMin())
                    .setHeightMax(queryVo.getHeightMax())
                    .setWeightMin(queryVo.getWeightMin())
                    .setWeightMax(queryVo.getWeightMax())
                    .setIncomeStatus(queryVo.getIncomeStatus())
                    .setProfession(queryVo.getProfession())
                    .setMaritalStatus(queryVo.getMaritalStatus())
                    .setCarHouseStatus(queryVo.getCarHouseStatus())
                    .setCode(queryVo.getCode())
                    .setUserCode(queryVo.getUserCode())
                    .setMemberStatus(queryVo.getMemberStatus());
            
            // 添加昵称和手机号查询条件
            if (queryVo.getNickname() != null && !queryVo.getNickname().isEmpty()) {
                userMembershipInfoVo.setNickname(queryVo.getNickname());
            }
            if (queryVo.getPhone() != null && !queryVo.getPhone().isEmpty()) {
                userMembershipInfoVo.setPhone(queryVo.getPhone());
            }
        }
        userMembershipInfoService.exportUserMembershipInfo(userMembershipInfoVo, response);
    }
}
